from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
import Ui_tenantSrc

class TenantSrc(QDialog, Ui_tenantSrc.Ui_tenantSrcDlg):
  def __init__(self, dbConn, parent=None):
        super(TenantSrc, self).__init__(parent)
        self.setupUi(self)
        self.db = dbConn
        
        self.srcModel = QSqlQueryModel(self)
        self.srcModel.setHeaderData(1, Qt.Horizontal, QVariant("Name"))
        self.srcModel.setHeaderData(2, Qt.Horizontal, QVariant("Address"))
        self.srcModel.setHeaderData(3, Qt.Horizontal, QVariant("City"))
        self.srcModel.setHeaderData(4, Qt.Horizontal, QVariant("Phone"))
        self.srcModel.setHeaderData(5, Qt.Horizontal, QVariant("HP"))
        self.srcModel.setHeaderData(6, Qt.Horizontal, QVariant("Company"))
        self.srcModel.setHeaderData(7, Qt.Horizontal, QVariant("Comp Addr"))
        self.srcModel.setHeaderData(8, Qt.Horizontal, QVariant("City"))
        self.srcModel.setHeaderData(9, Qt.Horizontal, QVariant("Email"))
        
        self.search_tbv.setModel(self.srcModel)
        self.search_tbv.setSelectionMode(QTableView.SingleSelection)
        self.search_tbv.setSelectionBehavior(QTableView.SelectRows)
        self.search_tbv.resizeColumnsToContents()
        
        self.id = ""
        
  @pyqtSignature("")
  def on_search_pb_clicked(self):
      qStr = "select kode_pemilik_penyewa, nama_pemilik_penyewa, alamat_pemilik_penyewa, kota_pemilik_penyewa, no_telp, no_ponsel, nama_pt, alamat_pt, kota_pt, email from master_pemilik_penyewa"
      filt = ""
      urut = " order by nama_pemilik_penyewa "
      if self.name_cb.checkState() == Qt.Checked:
          filt = filt + " nama_pemilik_penyewa like '"+self.name_le.text()+"%' and "
      if self.addr_cb.checkState() == Qt.Checked:
          filt = filt + " ( alamat_pemilik_penyewa like '"+self.addr_le.text()+"%' or kota_pemilik_penyewa like '"+self.addr_le.text()+"%' and "
          
      if filt != "":
          filt = " where "+filt.left(len(filt)-4)
          
      qStr = qStr + filt + urut
      self.srcModel.setQuery(qStr, self.db)
      self.search_tbv.setColumnHidden(0, True)
      self.search_tbv.setColumnWidth(2, 250)
      self.search_tbv.resizeRowsToContents()
      
  @pyqtSignature("")
  def on_choose_pb_clicked(self):
      index = self.search_tbv.currentIndex()
      if index.isValid():
          brs = self.srcModel.record(index.row())
          self.id = brs.value("kode_pemilik_penyewa").toString()
          self.done(1)
          
  @pyqtSignature("")
  def on_cancel_pb_clicked(self):
      self.close()
      
  def getID(self):
    return self.id
      
if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    form = TenantSrc()
    form.show()
    app.exec_()
